package com.itheima.web.listener;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class MyServletContextAttributeListener implements ServletContextAttributeListener {
    @Override // 当ServletContext添加一个属性后触发
    public void attributeAdded(ServletContextAttributeEvent event) {
        String name = event.getName();
        Object value = event.getValue();
        System.out.println("添加属性啦: " + name + " = " + value);
    }

    @Override // 当ServletContext删除一个属性后触发
    public void attributeRemoved(ServletContextAttributeEvent event) {
        String name = event.getName();
        Object value = event.getValue();
        System.out.println("删除属性啦: " + name + " = " + value);
    }

    @Override // 当ServletContext修改一个属性后触发
    public void attributeReplaced(ServletContextAttributeEvent event) {
        String name = event.getName();
        Object oldValue = event.getValue(); // 修改前的值

        Object newValue = event.getServletContext().getAttribute(name); // 修改后的值
        System.out.println("修改属性啦, 修改前: " + name + " = " + oldValue);
        System.out.println("修改属性啦, 修改后: " + name + " = " + newValue);
    }
}
